<?php
namespace Admin\Controller;
use Think\Controller;
class AuthController extends CommonController {
    public function cate(){
        $this->log('查看权限分类');
    	$cate = M('auth_cate')->select();
    	$this->assign('cate',$cate);
        $this->display();
    }
    public function cate_add()
    {

        $this->log('增加分类',1);

    	$data = I('post.');

    	$id = M('auth_cate')->add($data);

    	if($id){
    		$arr = array(
    			'error'=>0,
    			'info'=>'添加成功',
    			'id'=>$id
    			);
    		echo  json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'添加失败'
    			);
    		echo  json_encode($arr);
    	}
    }
    public function cate_del()
    {
        $id = I('post.id');
    	$res = M('auth_cate')->delete($id);
    	if($res){
    		$this->returnJson(0,'删除成功');
            $this->log('增加分类,删除id:'.$id,1);
    	}else{
    		$this->returnJson(1,'删除失败');
    	}
    }
    public function cate_edit()
    {
    	$id = I('get.id');
    	$cate = M('auth_cate')->select($id);
    	$this->assign('cate',current($cate));
    	$this->display();
    }

    public function cate_update()
    {
        
    	$data = I('post.');
    	$res = M('auth_cate')->save($data);

    	if($res){
    		$this->returnJson(0,'保存成功');
    	}else{
    		$this->returnJson(1,'保存失败');
    	}
    }

    public function cate_del_all()
    {
    	$ids = I('post.ids');

    	$res = M('auth_cate')->delete($ids);

    	if($res){
    		$this->returnJson(0,'删除成功');
    	}else{
    		$this->returnJson(1,'删除失败');
    	}


    }
    public function rules()
    {
    	$sql ="select t1.*,t2.cname from auth_rule t1 left join auth_cate t2 on t1.cid=t2.id";
    	$rules = M()->query($sql);
    	$cate = M('auth_cate')->select();
    	//分配变量
    	$this->assign('cate',$cate);
    	$this->assign('rules',$rules);
    	//引模板
    	$this->display();
    }
    public function rule_add()
    {
    	$data = I('post.');

    	$id = M('auth_rule')->add($data);

    	if($id){
    		$arr = array(
    			'error'=>0,
    			'info'=>'添加成功',
    			'id'=>$id
    			);
    		echo  json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'添加失败'
    			);
    		echo  json_encode($arr);
    	}
    }
     public function rule_del()
    {
    	$id = I('post.id');
    	$res = M('auth_rule')->delete($id);
    	if($res){
    		$this->returnJson(0,'删除成功');
    	}else{
    		$this->returnJson(1,'删除失败');
    	}
    }

    public function rule_del_all()
    {
    	$ids = I('post.ids');

    	$res = M('auth_rule')->delete($ids);

    	if($res){
    		$this->returnJson(0,'删除成功');
    	}else{
    		$this->returnJson(1,'删除失败');
    	}
    }

    public function rule_edit()
    {
    	$id = I('get.id');
    	$rule = M('auth_rule')->select($id);
    	$cate = M('auth_cate')->select();
    	//分配变量
    	$this->assign('cate',$cate);
    	$this->assign('rule',current($rule));
    	$this->display();
    }

    public function rule_update()
    {
    	$data = I('post.');
    	$res = M('auth_rule')->save($data);

    	if($res){
    		$this->returnJson(0,'保存成功');
    	}else{
    		$this->returnJson(1,'保存失败');
    	}
    }


    public function role()
    {
    	$this->display();
    }
    public function admin()
    {
    	$this->display();
    }
}